home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0985.arc / CROSSTH.LTG < prev    next >
Text File  |  1986-02-27  |  6KB  |  200 lines

  1.  
  2.                      CrossThoughts September 
  3.  
  4.                             Listings 
  5.  
  6.  
  7.                            Listinτá 1«á 
  8.  PP╠á codσ fo≥ clippinτ algorithφ usinτ thσ ninσ screeεá region≤ ì
  9.                              method.
  10.  
  11.  
  12. Screeεá limit≤á arσá Xmiε t∩ Xma° fo≥ thσ ╪ axi≤ anΣ Ymiε t∩ Yma° iεá thσá ┘ ì
  13. axis.
  14.  
  15. TYPE Video_Code = (Center, Left, Right, Top, Bottom)
  16.      Screen_Video = RECORD XL, YL : Video_Code END 
  17.  
  18. FUNCTION ScreenLoc(X, Y : real) return Screen_Code
  19.  
  20. A : Screen_Code
  21.  
  22. BEGIN
  23.     A.XL = Center; A.YL = Center
  24.     IF X < Xmin THEN A.XL = Left END IF
  25.     IF X > Xmax THEN A.XL = Right END IF
  26.     IF Y < Ymin THEN A.YL = Bottom END IF
  27.     IF Y > Ymax THEN A.YL = Top END IF
  28.     ScreenLoc = A
  29. END ScreenLoc
  30.  
  31. PROCEDURE ClipLine(Xb, Yb, Xf, Yf : real)
  32.  
  33.  
  34. PROCEDURE AdjustLine(Loc : Screen_Code) return BOOLEAN
  35.  
  36. BEGIN
  37.      IF Loc has Left code THEN X = Xmin; Calculate clipped Y for Xmin END IF
  38.      IF Loc has Right code THEN X = Xmax; Calculate clipped Y for Xmax END IF
  39.      IF Loc has Bottom code THEN Y = Ymin; Calculate clipped X for Ymin END IF 
  40.      IF Loc has Top code THEN Y = Ymax;  Calculate clipped X for Ymax END IF
  41.      IF Loc = Loc1 THEN Xb = X; Yb = Y ELSE Xf = X; Yf = Y END IF
  42.      IF clipping edge is successful THEN AdjustLine = TRUE 
  43.                                     ELSE AdjustLine = FALSE
  44. END AdjustLine
  45.  
  46. BEGIN
  47.     Loc1 = ScreenLoc(Xb, Yb); Loc2 = ScreenLoc(Xf, Yf)
  48.     Total_Visible_Flag = (Loc▒.XL = Center) AND (Loc1.YL = Center) AND
  49.                          (Loc2.XL = Center) AND (Loc2.YL = Center)
  50.     IF Not Total_Visible_Flag
  51.     THEN -- Attempt to determine if the line is trivially outside
  52.           Conditon_Y1 = Loc1.YL = Bottom AND Loc2.YL = Bottom
  53.           Conditon_Y1 = Loc1.YL = Top AND Loc2.YL = Top
  54.           Conditon_X1 = Loc1.XL = Left AND Loc2.XL = Left
  55.           Conditon_X2 = Loc1.XL = Right AND Loc2.XL = Rightè          Partial_Visible_Flag = NOT ( Condition_X1 OR  Condition_X2
  56.                                     OR Condition_Y1 OR Condition_Y2)
  57.     ELSE Partial_Visible_Flag = FALSE
  58.     END IF
  59.     IF Partial_Visible_Flag AND (NOT Total_Visible_Flag)
  60.     THE╬
  61.         OK = TRUE
  62.         IF NOT (Loc▒.XL = Center) AND (Loc1.YL = Center))  
  63.             THEN OK = AdjustLine(Loc1) END IF
  64.         IF NOT (Loc2.XL = Center) AND (Loc2.YL = Center)) AND OK
  65.             THEN OK = AdjustLine(Loc2) END IF   
  66.         Total_Visible_Flag = OK
  67.     END IF
  68.     IF Total_Visible_Flag THEN Draw line (Xb,Yb),(Xf,Yf)
  69. END ClipLine
  70.  
  71.  
  72.  
  73.                            Listinτá 2«á 
  74.  PP╠ codσ fo≥ calculating Bezier curve and performing 2D plots.
  75.  
  76.  
  77. FUNCTION Bezier(U : real; X : ARRAY [1..N] OF real) return real
  78.  
  79. -- The "!" symbol is used to indicate the factorial
  80. -- The "X" array represent the control points.
  81. -- U is the Bezier argument.
  82.  
  83. BEGIN
  84.      INITIALIZE: SumX = 0; 
  85.      LOOP
  86.      BEGIN For i = 0 to N
  87.           SumX += [N! / i! (n - i)!] * U^i * (1 - U)^(N - 1) * X[I]
  88.      END LOOP
  89.      TERMINATE: None
  90.      return( SumX )
  91. END Bezier
  92.  
  93.  
  94. PROCEDURE Draw_Bezier(X, Y : ARRAY [1..N] OF real;
  95.                       Start, Finish, Step : real)
  96.  
  97. BEGIN
  98.      INITIALIZE: Read X and Y array; Initialize screen
  99.                  U = Start
  100.      LOOP
  101.      BEGIN IF U > Finish THEN EXIT END IF
  102.           Xplot = Bezier(U,X); Yplot = Bezier(U,Y);
  103.           IF U = Start THEN Plot(Xplot,Yplot) 
  104.                        ELSE Moveto(Xplot,Yplot) 
  105.           END IF
  106.           U += Step
  107.      END LOOP
  108.      TERMINATE: None
  109. END Draw_Bezierè
  110.  
  111.  
  112.                            Listinτá3. 
  113.               PPL code for non-periodic B-splines.
  114.  
  115. FUNCTION Knot(J : integer) return integer
  116.  
  117. BEGIN
  118.      CASE j
  119.           WHEN < k => return (0)
  120.           WHEN (>= k) AND (<= n) => return (j - k + 1)
  121.           OTHERWISE return(n - k + 2)
  122.      END CASE
  123. END KNot
  124.  
  125.  
  126. FUNCTION Compose(i, k : integer; x : real) return real
  127.  
  128. BEGIN
  129.      IF k = 1
  130.      THEN
  131.           y = 0
  132.           IF (Knot(i) <= x) AND (x < Knot(i+1)) THEN y = 1 END IF
  133.      ELSE
  134.           y = 0
  135.           t = Knot(i + k - 1) - Knot(i)
  136.           IF t <> 0 THEN y = (x - Knot(i)) * COmpose(i,k-1,x)/t END IF
  137.           t = Knot(i+k) - Knot(i+1)
  138.           IF t <> 0 THEN y += Knot(i+k) - x)*Compose(i+1,k-1,x)/t
  139.      END IF
  140.      return (y)
  141. END Compose
  142.  
  143.  
  144. PROCEDURE Draw_Spline(X, Y : ARRAY [1..N] OF real;
  145.                       N, K : integer;
  146.                       U : real)
  147.  
  148. BEGIN
  149.      INITIALIZE: Read X and Y array; Initialize screen
  150.                  Xplot = o; Yplot = 0
  151.      LOOP
  152.      BEGIN For i = 0 to N
  153.           V = Compose(i,K,U)
  154.           Xplot += V * X[i]; Yplot += V * Y[i]
  155.           IF i = 0 THEN Plot(Xplot,Yplot) 
  156.                    ELSE Moveto(Xplot,Yplot) 
  157.           END IF
  158.      END LOOP
  159.      TERMINATE: None
  160. END Draw_Spline
  161.  
  162.  
  163. è                            Figure 2. 
  164.             Equations for basic shape manipulations.
  165.  
  166. Translation:
  167.  
  168.      X' = X + Dx X               
  169.      Y' = Y + Dy Y
  170.  
  171. where Dx and Dy are the displacement in the X 
  172.       and Y dimension, respectively.
  173.      
  174. Rotation:
  175.      
  176.      X' = X cos(A) + y sin(A)
  177.      Y' = -X sin(A) + Y cos(A)
  178.  
  179. where A = Angle of clockwise rotation about the origin.
  180.  
  181. Scaling:
  182.  
  183.      X' = Sx X
  184.      Y' = Sy Y
  185.  
  186. wherσ S° anΣ S∙ arσ thσ scalinτ factor≤ fo≥ 
  187.       X and Y dimension, respectively.
  188.      
  189. Reflection:
  190.  
  191.      X' = - X  (around Y axis)
  192.      Y' = - Y  (around X axis)
  193.  
  194. Shear:
  195.  
  196.      X' = X + S X      Y' = Y         (X shear)
  197.      X' = X            Y' = Y + S Y   (Y shear)
  198.  
  199. where S = shear factor.
  200.